package com.utilities;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;

public class TimeUtils {
	
	
	
	public static String addDays(String startDate,int amount) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(startDate));
		cal.add(Calendar.DAY_OF_MONTH, amount);
		long mills = cal.getTimeInMillis();
		Date date = new Date(mills);
		return sdf.format(date);
	}
	public static String addDays(int amount) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, amount);
		long mills = cal.getTimeInMillis();
		Date date = new Date(mills);
		return sdf.format(date);
	}
	
	/**
	 * @param starttime
	 * @param endtime
	 * @return
	 */
	public static String howLong(String starttime,String endtime) {
		long startseconds = new SimpleDateFormat("HH:mm").parse(
				starttime, new ParsePosition(0)).getTime();
		long endseconds =new SimpleDateFormat("HH:mm").parse(
				endtime, new ParsePosition(0)).getTime();
		
		//System.out.println("the start time is:" + starttime
		//		+ ",the end time is:" + endtime);
		long millonseconds = endseconds - startseconds;
		long minutes=millonseconds / (1000 * 60);
		String totaltime=null;
		if(minutes<60){
			totaltime=String.valueOf(minutes)+" 分钟";
		}else{
			long hours=millonseconds / (1000 * 60*60);
			long leftminutes=minutes-hours*60;
			totaltime=String.valueOf(hours)+"小时 "+String.valueOf(leftminutes)+"分钟";
		}
	//	String totalminutes = String.valueOf(millonseconds / (1000 * 60))+"分钟";
	  //  System.out.println("the total took time is:"+totalminutes);
		return totaltime;

	}
	/*
	 * return the string as english
	 */
	public static String getTrainDate(String stationdate) throws ParseException{
		SimpleDateFormat curtime= new SimpleDateFormat("HH:mm:ss");
		String nowtime=curtime.format(Calendar.getInstance().getTime());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String source=stationdate+" "+nowtime;
		SimpleDateFormat utctime=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz+0800 yyyy",Locale.ENGLISH);
		utctime.setTimeZone(TimeZone.getTimeZone("UTC"));
		return utctime.format(sdf.parse(source));
	}
	
   public static String getTime(){
		
		String returntime=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Calendar.getInstance().getTime());
		return returntime;
	}
	
	public static int randomnumber(int min,int max){
		
		   Random rand = new Random();

		    // nextInt is normally exclusive of the top value,
		    // so add 1 to make it inclusive
		    int randomNum = rand.nextInt((max - min) + 1) + min;

		    return randomNum;
	}
	public static void main(String[] args) throws Exception {
		//String returntime=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Calendar.getInstance().getTime());
	
	    String traindate=getTrainDate("2014-02-15");
	   System.out.println("date is:"+traindate);
	}

}
